//
//  LS_DestinationView.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/18.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_DestinationView.h"
#import <iCarousel.h>
#import "LS_DestinVideoModel.h"
#define PAGE_OFFSET 10
@interface LS_DestinationView()<iCarouselDelegate,iCarouselDataSource>
@property(nonatomic,strong)iCarousel *carousel;
@property(nonatomic,strong)UIImageView *placeHolderImageView;

/**
 model 数组
 */
@property(nonatomic,strong)NSMutableArray *modelGroup;
@end

@implementation LS_DestinationView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self carousel];
        
        
    }return self;
}
-(void)setAutoscrollTimeInterval:(CGFloat)autoscrollTimeInterval
{
    _autoscrollTimeInterval = autoscrollTimeInterval;
    self.carousel.autoscroll = autoscrollTimeInterval;
    
}
-(void)setPlaceHold:(NSString *)placeHold
{
    _placeHold = placeHold;
    [self bringSubviewToFront:self.placeHolderImageView];
    UIImage *placeholdImage = [UIImage imageNamed:placeHold];
    self.placeHolderImageView.image = [placeholdImage HT_setRadius:self.cornerRadius size:CGSizeMake(ScreenWidth - 2*PAGE_OFFSET, self.height)];
}
-(UIImageView *)placeHolderImageView
{
    return LS_LAZY(_placeHolderImageView, ({
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth - PAGE_OFFSET * 2, self.height)];
        imageView.contentMode = UIViewContentModeScaleToFill;
        [self addSubview:imageView];
        imageView;
    }));
}
-(iCarousel *)carousel
{
    return LS_LAZY(_carousel, ({
        iCarousel *carouse = [[iCarousel alloc]init];
        carouse.delegate   = self;
        carouse.dataSource = self;
        carouse.pagingEnabled = YES;
        carouse.bounces = YES;
        carouse.scrollSpeed = 1;
        carouse.type = iCarouselTypeRotary;
        [self addSubview:carouse];
        carouse;
    }));
}
-(NSMutableArray *)modelGroup
{
    return LS_LAZY(_modelGroup, @[].mutableCopy);
}
-(void)setModelSignal:(RACSignal *)modelSignal
{
    _modelSignal = modelSignal;
    @weakify(self)
    [_modelSignal subscribeNext:^(id x) {
        @strongify(self)
        if ([x count] > 0) {
            for (LS_DestinVideoModel *model in x) {
                [self.modelGroup addObject:model];
            }
            
            LS_DestinVideoModel *Model = [LS_DestinVideoModel new];
            Model.cover = @"VideoPlaceholder";
            [self.modelGroup addObject:Model];
         }
        
        dispatch_main_async_safe(^{
            [self.carousel reloadData];
        });
    }];
    
    
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.carousel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
}
- (void)dealloc
{
    self.carousel.dataSource = nil;
    self.carousel.delegate = nil;
}
#pragma mark - iCarouselDelegate && iCarouselDataSource
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.modelGroup.count;
}
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    CGFloat viewWidth = (SCREEN_WIDTH/375)*300;
    CGFloat viewHeight = (SCREEN_HEIGHT/667)*200;
    UILabel *titleLabel = nil;
    UILabel *likeLabel = nil;
    UIImageView *startView = nil;
    if (view == nil) {
        
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight)];
        view.backgroundColor = [UIColor clearColor];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, viewWidth-16, 50)];
        titleLabel.numberOfLines = 0;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = LS_FontWithname(@"DamascusBold", 15);
        [view addSubview:titleLabel];
        
        likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, viewHeight - 30, viewWidth-16, 20)];
        likeLabel.textColor = [UIColor whiteColor];
        likeLabel.font = LS_FontWithname(@"Damascus", 12);
        [view addSubview:likeLabel];
        
        startView = [[UIImageView alloc] initWithFrame:CGRectMake(viewWidth - 40, viewHeight-40, 30, 30)];
        [view addSubview:startView];
    }
    
    LS_DestinVideoModel *model = self.modelGroup[index];
    if ([model.cover hasPrefix:@"http"]) {
        
        [((UIImageView *)view) HT_setImageWithCornerRadius:5 imageURL:[NSURL URLWithString:model.cover] placeholder:nil contentMode:UIViewContentModeScaleToFill size:CGSizeMake(viewWidth, viewHeight)];
        titleLabel.text = model.product_title;
        likeLabel.text = [NSString stringWithFormat:@"%@个面粉看过",model.views];
        startView.image = [UIImage imageNamed:@"btn_start_20x20_"];
        
    }else{
        
        UIImage *placeholderImage = [UIImage imageNamed:model.cover];
        ((UIImageView *)view).image = [placeholderImage HT_setRadius:5 size:CGSizeMake(viewWidth, viewHeight)];
    }
    
    return view;
}
- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0.0, 1.0, 0.0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * self.carousel.itemWidth);
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    
    LS_DestinVideoModel *Model = self.modelGroup[index];
//    if ([Model.show_url isNotBlank]) {
//
//        HTWebViewModel *viewModel = [[HTWebViewModel alloc] initWithServices:nil params:@{WebTitlekey:@"",RequestURLkey:Model.show_url,WebNavBarStyleTypekey:@(kWebNavBarStyleHidden)}];
//        [[HTMediatorAction sharedInstance] pushWebViewControllerWithViewModel:viewModel];
//
//    }else{
//
//        HTViewModelServicesImpl *servicesImpl = [[HTViewModelServicesImpl alloc] initModelServiceImpl];
//        HTExploreMoreViewModel *viewModel = [[HTExploreMoreViewModel alloc] initWithServices:servicesImpl];
//        [[HTMediatorAction sharedInstance] pushExploreMoreViewControllerWithViewModel:viewModel];
//
//    }
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    
}

@end
